/*****************************************************************************
 * ui_sys_body.c: Copyright 2023 (c) CoreLink
 * Desc:
 *
 * Author: Flying (weekend@111.com)
 * Date: 2023-09-06 18:05:42
 * Notes:
 *
 * ---------------------------------------------------------------------------
 * LastEditors:
 * LastEditTime:
 *****************************************************************************/

/*------------------------------- Includes ----------------------------------*/
#include "ui_sys_body.h"
#include "ui_common.h"

/*------------------- 全局定义和声明 -------------------*/

/*---------------------- 常量/宏定义 -----------------------*/
#define CONT_W_MAX (320)
#define CONT_H_MAX (420)

#define ITEM_SPACE_W (32)
#define ITEM_SPACE_H (32)

#define ITEM_W ((CONT_W_MAX - ITEM_SPACE_W * (info->col_max + 1)) / info->col_max)
#define ITEM_H ((CONT_H_MAX - ITEM_SPACE_H * (info->row_max + 1)) / info->row_max)

/*----------------------- 类型声明 ---------------------------------*/
typedef struct ui_sys_body_lv_node_t_
{
    lv_obj_t *obj;
    LIST_HEAD list;
} ui_sys_body_lv_node_t;

/*----------------------- 变量声明 -----------------------------*/

/*----------------------- 函数原型 --------------------------------*/
static void ui_sys_body_scroll_event_cb(lv_event_t *e);

/*----------------------- 功能实现 --------------------------------*/
ui_sys_body_info_t *ui_sys_body_create(lv_obj_t *parent, ui_sys_body_event_cb_t cb, void *arg)
{
    ui_sys_body_info_t *info = (ui_sys_body_info_t *)MY_MALLOC(sizeof(ui_sys_body_info_t));
    memset(info, 0, sizeof(ui_sys_body_info_t));
    info->parent = parent;
    info->event_cb = cb;
    info->cb_arg = arg;
    INIT_LIST_HEAD(&info->page_list);
    INIT_LIST_HEAD(&info->item_list);

    return info;
}

int ui_sys_body_show(ui_sys_body_info_t *info)
{

    info->cont = lv_obj_create(info->parent);
    info->row_max = 2; // 行数
    info->col_max = 2; // 列数
    if (info->row_max <= 0)
    { // 限制最小，一行两列
        info->row_max = 1;
    }
    if (info->col_max <= 1)
    {
        info->col_max = 2;
    }

    lv_obj_set_size(info->cont, CONT_W_MAX, CONT_H_MAX);
    lv_obj_set_scroll_snap_x(info->cont, LV_SCROLL_SNAP_CENTER);
    lv_obj_set_flex_flow(info->cont, LV_FLEX_FLOW_ROW);
    lv_obj_align(info->cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_scroll_dir(info->cont, LV_DIR_HOR);
    lv_obj_set_style_radius(info->cont, 0, 0);
    lv_obj_add_flag(info->cont, LV_OBJ_FLAG_SCROLLABLE);   // 可滑动
    lv_obj_add_flag(info->cont, LV_OBJ_FLAG_SCROLL_CHAIN); // 可滑动
    lv_obj_add_flag(info->cont, LV_OBJ_FLAG_SCROLL_ONE);
    lv_obj_set_style_opa(info->cont, LV_OPA_0, LV_PART_SCROLLBAR); // 滑动条隐藏
    lv_obj_set_style_pad_all(info->cont, 0, 0);
    lv_obj_set_style_border_width(info->cont, 0, 0);
    lv_obj_set_style_bg_color(info->cont, lv_color_hex(0xff0000), 0);
    lv_obj_set_style_bg_opa(info->cont, LV_OPA_0, 0);
    lv_obj_add_event_cb(info->cont, ui_sys_body_scroll_event_cb, LV_EVENT_SCROLL_END, info);

    // 小图标底图
    lv_style_init(&info->page_style);
    lv_style_set_x(&info->page_style, 0);
    lv_style_set_y(&info->page_style, 0);
    lv_style_set_width(&info->page_style, CONT_W_MAX);
    lv_style_set_height(&info->page_style, CONT_H_MAX);
    lv_style_set_pad_all(&info->page_style, 0);
    lv_style_set_border_width(&info->page_style, 0);
    lv_style_set_radius(&info->page_style, 0);
    lv_style_set_bg_color(&info->page_style, lv_color_hex(0xffff00));
    lv_style_set_bg_opa(&info->page_style, LV_OPA_0);
    return 0;
}

int ui_sys_body_delete(ui_sys_body_info_t *info)
{
    // TODO
    return -1;
}

/**
 * @description: 滑动事件
 * @param {lv_event_t} *e
 * @return {*}
 */
static void ui_sys_body_scroll_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    ui_sys_body_info_t *info = (ui_sys_body_info_t *)lv_event_get_user_data(e);
    lv_obj_t *cont = lv_event_get_target(e);

    if (code != LV_EVENT_SCROLL_END)
    {
        return;
    }

    lv_point_t p;
    lv_obj_get_scroll_end(cont, &p);
    info->sync_page = p.x / (CONT_W_MAX);
    info->sync_code = SYS_BODY_CODE_SCROLL;
    // LV_LOG_USER("page = %d\n", index);
    if (info->event_cb)
    {
        info->event_cb(info);
    }
}
/**
 * @description: 获取第几页，如果不存在则创建
 * @param {*} 0开始
 * @return {*}
 */
static lv_obj_t *ui_sys_body_get_page(ui_sys_body_info_t *info, uint8_t index)
{
    LIST_HEAD *node = NULL;
    ui_sys_body_lv_node_t *lv_node = NULL;
    uint8_t tmp_index = 0;

    tuya_list_for_each(node, &info->page_list)
    {
        if (tmp_index == index)
        {
            lv_node = tuya_list_entry(node, ui_sys_body_lv_node_t, list);
            return lv_node->obj;
        }
        tmp_index++;
    }
    lv_obj_t *obj = lv_obj_create(info->cont);
    lv_obj_add_style(obj, &info->page_style, 0);
    lv_obj_set_style_opa(obj, LV_OPA_0, LV_PART_SCROLLBAR); // 滑动条隐藏
    lv_obj_set_style_pad_all(obj, 0, 0);
    lv_obj_set_style_border_width(obj, 0, 0);

    lv_node = (ui_sys_body_lv_node_t *)MY_MALLOC(sizeof(ui_sys_body_lv_node_t));
    memset(lv_node, 0, sizeof(ui_sys_body_lv_node_t));
    lv_node->obj = obj;

    INIT_LIST_HEAD(&lv_node->list);

    tuya_list_add_tail(&lv_node->list, &info->page_list);

    return obj;
}

/**
 * @description: 点击事件+动效
 * @param {lv_event_t} *e
 * @return {*}
 */
static void ui_sys_body_item_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    ui_sys_body_info_t *info = (ui_sys_body_info_t *)lv_event_get_user_data(e);
    lv_obj_t *obj = lv_event_get_target(e);
    if (code == LV_EVENT_GESTURE)
    {
        lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());

        switch (dir)
        {
        case LV_DIR_LEFT:
            // printf("LV_DIR_LEFT\r\n");
            break;
        case LV_DIR_RIGHT:
            // printf("LV_DIR_RIGHT\r\n");
            break;
        case LV_DIR_TOP:
            info->sync_index = *((int *)&obj->user_data);
            info->sync_code = SYS_BODY_CODE_GESTURE_UP;
            if (info->event_cb)
            {
                info->event_cb(info);
            }
            lv_obj_add_flag(obj, LV_OBJ_FLAG_USER_2);
            break;
        case LV_DIR_BOTTOM:
            // printf("LV_DIR_BOTTOM\r\n");
            break;
        default:
            break;
        }
        return;
    }

    if (code == LV_EVENT_PRESSED)
    {
        // 事件响应改为Item_bgobj，修改效果
        //  lv_rlottie_set_play_mode(obj, LV_RLOTTIE_CTRL_LOOP);
        lv_obj_set_style_bg_opa(obj, LV_OPA_40, 0);
        lv_obj_clear_flag(obj, LV_OBJ_FLAG_USER_2);
        return;
    }
    if (code == LV_EVENT_RELEASED)
    {
        // 事件响应改为Item_bgobj，修改效果
        //  lv_rlottie_set_play_mode(obj, LV_RLOTTIE_CTRL_PAUSE);
        lv_obj_set_style_bg_opa(obj, LV_OPA_20, 0);
        return;
    }

    if (code != LV_EVENT_CLICKED)
    {
        return;
    }

    if (lv_obj_has_flag(obj, LV_OBJ_FLAG_USER_2))
    {
        return;
    }

    info->sync_index = *((int *)&obj->user_data);
    info->sync_code = SYS_BODY_CODE_CLICKED;
    if (info->event_cb)
    {
        info->event_cb(info);
    }
    // LV_LOG_USER("CLICKED %d", index);
}

/**
 * @description: 4列2行（800/4=200 ，400/2=200）(上下空出40)
 * @param {*}
 * @return {*}
 */
lv_obj_t *ui_sys_body_add_item(ui_sys_body_info_t *info, const lv_img_dsc_t *img_dsc, char *label_text, bool is_draw_line, bool is_enable)
{
    int count = 0;
    LIST_HEAD *node = NULL;
    tuya_list_for_each(node, &info->item_list)
    {
        count++;
    }

    int page_index = count / (info->row_max * info->col_max);
    int pos = count % (info->row_max * info->col_max); // 当前页的坐标
    int col_pos = pos % info->col_max;                 // 第几列
    int row_pos = pos / info->col_max;                 // 第几行

    lv_obj_t *item_parent = ui_sys_body_get_page(info, page_index);

    int icon_x = ITEM_SPACE_W + col_pos * (ITEM_W + ITEM_SPACE_W);
    int icon_y = ITEM_SPACE_H + row_pos * (ITEM_H + ITEM_SPACE_H);

    PR_DEBUG("flying count = %d ", count);
    // 添加item bg cont
    lv_obj_t *Item_bgobj = lv_obj_create(item_parent);
    lv_obj_set_style_bg_color(Item_bgobj, lv_color_hex(0x1d1d1d), 0);
    lv_obj_set_style_bg_opa(Item_bgobj, LV_OPA_20, 0);
    lv_obj_set_pos(Item_bgobj, icon_x, icon_y);
    lv_obj_set_style_radius(Item_bgobj, 10, 0);
    lv_obj_set_size(Item_bgobj, 120, 120);
    lv_obj_set_style_border_width(Item_bgobj, 0, 0);
    lv_obj_set_style_pad_all(Item_bgobj, 0, 0);

    // 修改为png图标
    lv_obj_t *obj = lv_img_create(Item_bgobj);
    lv_img_set_src(obj, img_dsc);
    lv_obj_align_to(obj, Item_bgobj, LV_ALIGN_CENTER, 0, -10);
    lv_obj_set_style_pad_all(obj, 0, 0);
    // lv_obj_set_pos(obj, icon_x, icon_y);

    // 将事件添加到Item_bgobj
    Item_bgobj->user_data = (void *)count;

    if (is_draw_line)
    {
        lv_obj_t *line = lv_line_create(item_parent);
        static lv_point_t p[] = {{0, 0}, {0, 0}};
        p[1].x = (short int)(ITEM_W / 3);
        lv_line_set_points(line, p, 2);
        lv_obj_set_style_line_width(line, 2 < 1 ? 1 : 2, 0);
        lv_obj_set_style_line_color(line, lv_palette_main(LV_PALETTE_RED), 0);
        lv_obj_align_to(line, obj, LV_ALIGN_OUT_BOTTOM_MID, 0, 18);
    }

    // int label_x = icon_x - ITEM_SPACE_W / info->row_max;
    // int label_y = icon_y + ITEM_H;
    lv_obj_t *label = lv_label_create(item_parent);
    // lv_obj_set_style_text_font(label, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_font(label, ui_font_get_font(16), 0);
    lv_obj_set_size(label, 120, 18);
    lv_obj_align_to(label, obj, LV_ALIGN_OUT_BOTTOM_MID, 0, -10);
    lv_obj_set_style_pad_top(label, 0, 0);
    // lv_obj_set_pos(label, label_x - 10, label_y - 40);
    lv_label_set_long_mode(label, LV_LABEL_LONG_DOT);
    lv_label_set_text(label, label_text);
    lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);

    // 将事件添加到Item_bgobj
    lv_obj_add_event_cb(Item_bgobj, ui_sys_body_item_event_cb, LV_EVENT_PRESSED, info);
    lv_obj_add_event_cb(Item_bgobj, ui_sys_body_item_event_cb, LV_EVENT_RELEASED, info);
    lv_obj_add_event_cb(Item_bgobj, ui_sys_body_item_event_cb, LV_EVENT_CLICKED, info);
    lv_obj_clear_flag(Item_bgobj, LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(Item_bgobj, ui_sys_body_item_event_cb, LV_EVENT_GESTURE, info);
    if (is_enable)
    {
        lv_obj_add_flag(Item_bgobj, LV_OBJ_FLAG_CLICKABLE);
    }
    else
    {
        lv_obj_clear_flag(Item_bgobj, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_add_flag(Item_bgobj, LV_STATE_DISABLED);
        lv_obj_set_style_bg_opa(Item_bgobj, LV_OPA_80, 0);
    }
    ui_sys_body_lv_node_t *lv_node = NULL;
    lv_node = (ui_sys_body_lv_node_t *)MY_MALLOC(sizeof(ui_sys_body_lv_node_t));
    memset(lv_node, 0, sizeof(ui_sys_body_lv_node_t));
    lv_node->obj = Item_bgobj;

    INIT_LIST_HEAD(&lv_node->list);
    tuya_list_add_tail(&lv_node->list, &info->item_list);

    return Item_bgobj;
}

int ui_sys_body_get_sync_code(ui_sys_body_info_t *info)
{

    return info->sync_code;
}

int ui_sys_body_get_sync_index(ui_sys_body_info_t *info)
{
    return info->sync_index;
}
/**
 * @description: 跳转到哪页
 * @param {int} index
 * @param {lv_anim_enable_t} anim_en
 * @return {*}
 */
void ui_sys_body_select_page(ui_sys_body_info_t *info, int index, lv_anim_enable_t anim_en)
{
    LIST_HEAD *node = NULL;
    ui_sys_body_lv_node_t *lv_node = NULL;
    uint8_t tmp_index = 0;

    tuya_list_for_each(node, &info->page_list)
    {
        if (tmp_index == index)
        {
            lv_node = tuya_list_entry(node, ui_sys_body_lv_node_t, list);
            lv_obj_scroll_to_view(lv_node->obj, anim_en);
        }
        tmp_index++;
    }
}
/**
 * @description: 获取图标obj
 * @param {int} index
 * @return {*}
 */
lv_obj_t *ui_sys_body_get_item_obj(ui_sys_body_info_t *info, int index)
{
    LIST_HEAD *node = NULL;
    ui_sys_body_lv_node_t *lv_node = NULL;
    uint8_t tmp_index = 0;

    tuya_list_for_each(node, &info->item_list)
    {
        if (tmp_index == index)
        {
            lv_node = tuya_list_entry(node, ui_sys_body_lv_node_t, list);
            return lv_node->obj;
        }
        tmp_index++;
    }
    return NULL;
}

/*---------------------------------------------------------------------------*/